﻿unit PmItemOtherService;

interface
{$I ../Config.inc}
uses
  FastMM4,
  FastMove,
  System.SysUtils,
  System.Classes,
  System.Types,
  Winapi.Windows,
  Data.DB,
  Data.Win.ADODB,
  Datasnap.DBClient,
  Datasnap.Provider,
  {$IFDEF UNICODE}
  AnsiStrings,
  {$ENDIF }
  QBParcel,
  DllSpread,
  Generics.Collections,
  DateUtils,
  QJson,
  UserConnection,
  DBAccessor,
  DllPlugin,
  StrUtils,
  SK0001Comm,
  PmItemOtherModel,
  PmItemOtherBLL,
  PmItemClassModel,
  PmItemDataModel,
  PmItemClassService,
  PmItemDataService;
  
type


  TPmItemOtherService = class(TPmItemOtherBLL)
  private

  protected

  public
  {$IFDEF SERVER}
    function BeforeTreatment(const iItem:TPmItemOther; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function BeforeTreatment(const iItems:TPmItemOtherList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;

    function BeforeCheck(const iItem:TPmItemOther; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function BeforeCheck(const iItems:TPmItemOtherList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;

    function Check(const iItem:TPmItemOther; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function Check(const iItems:TPmItemOtherList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;

    function BeforeExecute(const iItem:TPmItemOther; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function BeforeExecute(const iItems:TPmItemOtherList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;

    function AfterTreatment(const iItem:TPmItemOther; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function AfterTreatment(const iItems:TPmItemOtherList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
  {$ENDIF}
    function LoadByItemClassId(const iItemClassId:string; const iOrgId:string; const iItemOthers:TPmItemOtherList; const iItemDatas:TPmItemDataList; out oError:string; const iJustUse:Boolean=False; const iIncludeChild:Boolean=False):Boolean;
    function LoadByItemNo(const iItemNo:string; const iItem:TPmItemOther; out oError:string):Boolean;
    function LoadAll(const iOrgId:string; const iItems:TPmItemOtherList; out oError:string; const iJustUse:Boolean=False):Boolean;
    function Add(const iItemOther:TPmItemOther; const iItemData:TPmItemData; out oError:string; const dIsCheck:Boolean=True):Boolean;
    function Edit(const iItemOther:TPmItemOther; const iItemData:TPmItemData; out oError:string; const dIsCheck:Boolean=True):Boolean;
    function Use(const iItem:TPmItemOther; out oError:string):Boolean;
  end;
  
implementation

{$IFDEF SERVER}
function TPmItemOtherService.BeforeTreatment(const iItem:TPmItemOther; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TPmItemOtherService.BeforeTreatment(const iItems:TPmItemOtherList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TPmItemOtherService.BeforeCheck(const iItem:TPmItemOther; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TPmItemOtherService.BeforeCheck(const iItems:TPmItemOtherList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TPmItemOtherService.Check(const iItem:TPmItemOther; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TPmItemOtherService.Check(const iItems:TPmItemOtherList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TPmItemOtherService.BeforeExecute(const iItem:TPmItemOther; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TPmItemOtherService.BeforeExecute(const iItems:TPmItemOtherList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TPmItemOtherService.AfterTreatment(const iItem:TPmItemOther; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TPmItemOtherService.AfterTreatment(const iItems:TPmItemOtherList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;
{$ENDIF}

function TPmItemOtherService.LoadByItemClassId(const iItemClassId:string; const iOrgId:string; const iItemOthers:TPmItemOtherList; const iItemDatas:TPmItemDataList; out oError:string; const iJustUse:Boolean=False; const iIncludeChild:Boolean=False):Boolean;
var
  Condition,OrderBy,Ids,Key:string;
  ItemClassService:TPmItemClassService;
  ItemDataService:TPmItemDataService;
  ItemClassItem:TPmItemClass;
  ItemClassItems:TPmItemClassList;
begin
  if not SameStr(iItemClassId,EmptyStr) and
    not SameStr(iOrgId,EmptyStr) and
    (iItemOthers<>nil) and
    (iItemDatas<>nil) then
  begin
    if iIncludeChild then
    begin
    {$IFDEF SERVER}
      ItemClassService:=TPmItemClassService.Create(fServConn,fDBConn,fDBID);
    {$ENDIF}
    {$IFDEF CLIENT}
      ItemClassService:=TPmItemClassService.Create(fServConn);
    {$ENDIF}
      ItemClassItem:=TPmItemClass.Create;
      ItemClassItems:=TPmItemClassList.Create;
      try
        if not ItemClassService.LoadById(iItemClassId,ItemClassItem,oError) then
        begin
          oError:=Format(string('调用TPmItemOtherService.LoadByItemClassId方法失败！错误内容:%s'),[]);
          Result:=False;
          Exit;
        end;
        Condition:=Format(string('%s LIKE ''%s'' and %s=%d '),[TPmItemClassModel.cItemClassTreeIdKey,ItemClassItem.ItemClassTreeId+'%',
                                                              TPmItemClassModel.cItemClassStatusKey,1]);
        OrderBy:=TPmItemClassModel.cItemClassTreePidKey+','+TPmItemClassModel.cItemClassSortNoKey+' ASC';

        if not ItemClassService.LoadByCondition(ItemClassItems,Condition,oError) then
        begin
          oError:=Format(string('调用TPmItemOtherService.LoadByItemClassId方法失败！错误内容:%s'),[]);
          Result:=False;
          Exit;
        end;
        Ids:=EmptyStr;
        for Key in ItemClassItems.SortKeys do
        begin
          Ids:=Ids+''''+ItemClassItems.Items[Key].ItemClassId+''',';
        end;
        if Ids.Length>0 then
        begin
          Ids:=LeftStr(Ids,Ids.Length-1);
          Condition:=Format(string('%s=''%s'' and %s IN (%s) '),[TPmItemOtherModel.cOrgIdKey,iOrgId,
                                                            TPmItemClassModel.cItemClassIdKey,Ids]);
        end else begin
          Condition:='1=0 ';
        end;
      finally
        ItemClassService.Free;
        ItemClassItem.Free;
        ItemClassItems.Free;
      end;
    end else begin
      Condition:=Format(string('%s=''%s'' and %s=''%s'' '),[TPmItemOtherModel.cOrgIdKey,iOrgId,
                                                            TPmItemClassModel.cItemClassIdKey,iItemClassId]);
    end;
    if iJustUse then
    begin
      Condition:=Condition+Format(string(' and %s=%d '),[TPmItemOtherModel.cItemStatusKey,1]);
    end;
    OrderBy:=TPmItemClassModel.cItemClassTreePidKey+','+TPmItemClassModel.cItemClassSortNoKey+','+TPmItemOtherModel.cItemSortNoKey+' ASC';

    if not LoadByCondition(iItemOthers,Condition,oError,OrderBy) then
    begin
      oError:=Format(string('调用TPmItemOtherService.LoadByItemClassId方法失败！错误内容:%s'),[]);
      Result:=False;
      Exit;
    end;

    Ids:=EmptyStr;
    for Key in iItemOthers.SortKeys do
    begin
      Ids:=Ids+''''+iItemOthers.Items[Key].ItemNo+''',';
    end;

    if Ids.Length>0 then
    begin
      Ids:=LeftStr(Ids,Ids.Length-1);
      Condition:=Format(string('%s IN (%s) '),[TPmItemDataModel.cItemNoKey,Ids]);
    end else begin
      Condition:='1=0 ';
    end;
    OrderBy:=TPmItemClassModel.cItemClassTreePidKey+','+TPmItemClassModel.cItemClassSortNoKey+','+TPmItemDataModel.cItemSortNoKey+' ASC';

  {$IFDEF SERVER}
    ItemDataService:=TPmItemDataService.Create(fServConn,fDBConn,fDBID);
  {$ENDIF}
  {$IFDEF CLIENT}
    ItemDataService:=TPmItemDataService.Create(fServConn);
  {$ENDIF}
    try
      if not ItemDataService.LoadByCondition(iItemDatas,Condition,oError,OrderBy) then
      begin
        oError:=Format(string('调用TPmItemOtherService.LoadByItemClassId方法失败！错误内容:%s'),[]);
        Result:=False;
        Exit;
      end;
    finally
      ItemDataService.Free;
    end;
  end else begin
    oError:=Format(string('调用TPmItemOtherService.LoadByItemClassId方法失败！错误内容:传入的iItemClassId或iOrgId或iItemOthers或iItemDatas为空!'),[]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TPmItemOtherService.LoadByItemNo(const iItemNo:string; const iItem:TPmItemOther; out oError:string):Boolean;
var
  Condition:string;
begin
  if not SameStr(iItemNo,EmptyStr) and (iItem<>nil) then
  begin
    Condition:=Format(string('%s=''%s'''),[TPmItemOtherModel.cItemNoKey,iItemNo]);

    if not LoadByCondition(iItem,Condition,oError) then
    begin
      Result:=False;
      Exit;
    end;
  end else begin
    oError:=Format(string('调用TPmItemOtherService.LoadByItemNo方法失败！错误内容:传入的iItemNo或iItem为空!'),[]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TPmItemOtherService.LoadAll(const iOrgId:string; const iItems:TPmItemOtherList; out oError:string; const iJustUse:Boolean):Boolean;
var
  Condition,OrderBy:string;
begin
  if (not SameStr(iOrgId,EmptyStr)) and (iItems<>nil) then
  begin
    if iJustUse then
    begin

      Condition:=Format(string('%s=''%s'' and %s=1'),[TPmItemOtherModel.cOrgIdKey,iOrgId,TPmItemOtherModel.cItemStatusKey]);
    end else begin
      Condition:=Format(string('%s=''%s'''),[TPmItemOtherModel.cOrgIdKey,iOrgId]);
    end;
    OrderBy:=TPmItemOtherModel.cItemNoKey+' ASC';

    if not LoadByCondition(iItems,Condition,oError,OrderBy) then
    begin
      Result:=False;
      Exit;
    end;
  end else begin
    oError:=Format(string('调用%s.LoadAll方法失败！错误内容:传入的iOrgId或iItems为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TPmItemOtherService.Add(const iItemOther:TPmItemOther; const iItemData:TPmItemData; out oError:string; const dIsCheck:Boolean=True):Boolean;
var
  ItemDataService:TPmItemDataService;
  SQLs:TStrings;
  SQL:string;
  Cds:TClientDataSet;
  MaxSortNo:Integer;
begin
  if (iItemOther<>nil) and (iItemData<>nil) then
  begin
  {$IFDEF SERVER}
    ItemDataService:=TPmItemDataService.Create(fServConn,fDBConn,fDBID);
  {$ENDIF}
  {$IFDEF CLIENT}
    ItemDataService:=TPmItemDataService.Create(fServConn);
  {$ENDIF}
    SQLs:=TStringList.Create;
    Cds:=TClientDataSet.Create(nil);
    try
      SQL:=Format(string('SELECT MAX(%s) AS %s FROM %s WHERE %s=''%s'';'),
              [TPmItemOtherModel.cItemSortNoKey,TPmItemOtherModel.cItemSortNoKey,
              TPmItemOtherModel.cTblName,TPmItemOtherModel.cOrgIdKey,
              iItemOther.OrgId]);
      if TSK0001Comm.GetCDS(fServConn,EmptyStr,SQL,Cds,oError) then
      begin
        if Cds.FieldByName(TPmItemOtherModel.cItemSortNoKey).IsNull then
        begin
          MaxSortNo:=0;
        end else begin
          MaxSortNo:=Cds.FieldByName(TPmItemOtherModel.cItemSortNoKey).AsInteger;
        end;
        iItemOther.ItemSortNo:=MaxSortNo+1;
      end else begin
        oError:='调用TPmItemOtherService.Add方法失败,错误内容:获取ItemOther最大SortNo失败!';
        Result:=False;
        Exit;
      end;

      SQL:=Format(string('SELECT MAX(%s) AS %s FROM %s WHERE %s=''%s'';'),
              [TPmItemDataModel.cItemSortNoKey,TPmItemDataModel.cItemSortNoKey,
              TPmItemDataModel.cTblName,TPmItemDataModel.cItemClassIdKey,
              iItemData.ItemClassId]);
      if TSK0001Comm.GetCDS(fServConn,EmptyStr,SQL,Cds,oError) then
      begin
        if Cds.FieldByName(TPmItemDataModel.cItemSortNoKey).IsNull then
        begin
          MaxSortNo:=0;
        end else begin
          MaxSortNo:=Cds.FieldByName(TPmItemDataModel.cItemSortNoKey).AsInteger;
        end;
        iItemData.ItemSortNo:=MaxSortNo+1;
      end else begin
        oError:='调用TPmItemOtherService.Add方法失败,错误内容:获取ItemData最大SortNo失败!';
        Result:=False;
        Exit;
      end;

      if inherited GetAddSQLs(iItemOther,SQLs,oError,dIsCheck) and
        ItemDataService.GetAddSQLs(iItemData,SQLs,oError,dIsCheck) then
      begin
        if not TSK0001Comm.ExecuteSQLs(fDBConn,EmptyStr,SQLs,oError) then
        begin
          oError:=Format(string('调用TPmItemOtherService.Add方法失败！错误内容:%s'),[oError]);
          Result:=False;
          Exit;
        end;
      end else begin
        oError:=Format(string('调用TCmCustomerFileService.Add方法失败！错误内容:%s'),[oError]);
        Result:=False;
        Exit;
      end;
    finally
      ItemDataService.Free;
      SQLs.Free;
      Cds.Free;
    end;
  end else begin
    oError:=Format(string('调用TCmCustomerFileService.Add方法失败！错误内容:传入的iItemOther或iItemData为空!'),[]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TPmItemOtherService.Edit(const iItemOther:TPmItemOther; const iItemData:TPmItemData; out oError:string; const dIsCheck:Boolean=True):Boolean;
var
  ItemDataService:TPmItemDataService;
  SQLs:TStrings;
begin
  if (iItemOther<>nil) and (iItemData<>nil) then
  begin
  {$IFDEF SERVER}
    ItemDataService:=TPmItemDataService.Create(fServConn,fDBConn,fDBID);
  {$ENDIF}
  {$IFDEF CLIENT}
    ItemDataService:=TPmItemDataService.Create(fServConn);
  {$ENDIF}
    SQLs:=TStringList.Create;
    try
      if inherited GetEditSQLs(iItemOther,SQLs,oError,dIsCheck) and
        ItemDataService.GetEditSQLs(iItemData,SQLs,oError,dIsCheck) then
      begin
        if not TSK0001Comm.ExecuteSQLs(fDBConn,EmptyStr,SQLs,oError) then
        begin
          oError:=Format(string('调用TPmItemOtherService.Edit方法失败！错误内容:%s'),[oError]);
          Result:=False;
          Exit;
        end;
      end else begin
        oError:=Format(string('调用TCmCustomerFileService.Edit方法失败！错误内容:%s'),[oError]);
        Result:=False;
        Exit;
      end;
    finally
      ItemDataService.Free;
      SQLs.Free;
    end;
  end else begin
    oError:=Format(string('调用TCmCustomerFileService.Edit方法失败！错误内容:传入的iItemOther或iItemData为空!'),[]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TPmItemOtherService.Use(const iItem:TPmItemOther; out oError:string):Boolean;
begin
  if (iItem<>nil) then
  begin
    if iItem.ItemStatus=1 then
    begin
      iItem.ItemStatus:=0;
      if not inherited Edit(iItem,oError) then
      begin
        oError:=Format(string('调用TPmItemOtherService.Use方法失败！错误内容:%s'),[oError]);
        Result:=False;
        Exit;
      end;
    end else begin
      iItem.ItemStatus:=1;
      if not inherited Edit(iItem,oError) then
      begin
        oError:=Format(string('调用TPmItemOtherService.Use方法失败！错误内容:%s'),[oError]);
        Result:=False;
        Exit;
      end;
    end;
  end else begin
    oError:=Format(string('调用TPmItemOtherService.Use方法失败！错误内容:传入的iItem为空!'),[]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;
  
end.
